/*
 This file 'Conditions' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2014-06-19 16:15
 */

define(function (require, exports, module) {

	var HashMap = require("Firebird.lang.HashMap");
	var Exception = require("Firebird.lang.Exception");
	var FormHelper = require("Firebird.ui.FormHelper");

	/**
	 * 数据容器 Firebird.data.Conditions
	 * @class Firebird.data.Conditions
	 * @extends Firebird.lang.HashMap
	 * @example
	 * <pre>
	 *     // Example 1:
	 *     var queryMap = new Firebird.data.Conditions({
     *          conditions: {
     *              type: "admin",
     *              name: ""
     *          }
     *     });
	 *
	 *     queryMap.put("status", "1");
	 *     queryMap.remove("name");
	 *
	 *     var store = new Firebird.data.Store({
     *         url: "{contextPath}/user/search",
     *         data: queryMap.getConditions()
     *     });
	 *
	 *     store.load();
	 *
	 *     firebird.info(store.getMap());
	 *
	 *     // Example 2:
	 *     var queryMap = new Firebird.data.Conditions({
     *          conditions: {
     *              type: "admin",
     *              name: "",
     *              count: 1
     *          },
     *          trigger: function(){
     *              this.put("count", this.get("count") + 1);
     *              this.put("org", "FBI");
     *          }
     *     });
	 *
	 *     firebird.info(queryMap.getConditions());
	 *     // count->1
	 *     firebird.info(queryMap.getConditions());
	 *     //
	 *     firebird.info(queryMap.getConditions());
	 *     //
	 * </pre>
	 */
	Firebird.define("Firebird.data.Conditions", {
		extend: "Firebird.lang.HashMap",

		statics: {
			_uuid: 1,
			uuid: function () {
				return this._uuid++;
			},
			attrs: function ($component) {

				return {

					renderTo: $component.get(0),
					// 实例ID, 可以在 context 里直接获取， var ins = context.getInstance("name");
					instanceId: $component.attr("fb-instance") || $component.attr("name") || $component.attr("id") || ("conditions_" + Firebird.ui.Conditions.uuid()),

					url: $component.attr("fb-url")

				};

			}
		},

		config: {
			conditions: null,
			trigger: null,
			formId: null,
			formHelper: null
		},

		formHelper: null,
		_init: function () {
			var me = this;
			var genId = me.genId = Firebird.Model.getGenId();

			if (this.getFormId()) {
				if (this.getFormId()) {
					this.setFormHelper(new FormHelper({
						id: this.getFormId()
					}));
				}
			}

			if (this.getFormHelper()) {
				var map = this.getFormHelper().serializeToObject()
				for (var name in map) {
					this.put(name, map[name]);
				}
			}

			if (this.conditions) {
				for (var key in this.conditions) {
					this.put(key, this.conditions[key]);
				}
			}
		},
		/**
		 * trigger
		 */
		trigger: function () {
		},
		/**
		 * 获取所有条件
		 * @returns {*}
		 */
		getConditions: function (filter) {
			if (this.getFormHelper()) {
				var map = this.getFormHelper().serializeToObject();
				for (var name in map) {
					this.put(name, map[name]);
				}
			}
			try {
				this.trigger.apply(this, [this.getMap()]);
			} catch (e) {
				new Exception({
					error: e
				});
			}
			var map = this.getMap();
			return filter && filter(map) || map;
		}
	});

	module.exports = Firebird.data.Conditions;

});